package com.yuerlive.rainbow.widget.game;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.DrawableRes;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.entity.Poker;
import com.yuerlive.rainbow.utils.DeviceUtils;


/**
 * Created by xiaoyi on 2017/5/27.
 */

public class PokerView extends LinearLayout {

    private int POKER_RED = getContext().getResources().getColor(R.color.poker_red);
    private int POKER_BLACK = getContext().getResources().getColor(R.color.poker_black);

    private TextView pokerValue;
    private ImageView pokerType;
    private ImageView back;

    private int defaultBack = R.drawable.poker_niuniu_back;
    private int pokerBack = defaultBack;

    public PokerView(Context context) {
        super(context);
        init(context,null);
    }

    public PokerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public PokerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context ,AttributeSet attrs) {
        if (attrs!=null) {
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PokerView);
            pokerBack = array.getResourceId(R.styleable.PokerView_back, defaultBack);
            array.recycle();
        }
        View.inflate(getContext(), R.layout.widget_poker, this);
        pokerValue = (TextView) findViewById(R.id.pokerValue);
        pokerType = (ImageView) findViewById(R.id.pokerType);
        back = (ImageView) findViewById(R.id.pokerBack);
        back.setImageResource(pokerBack);
    }

    public void setPokerValue(Poker poker) {
        if (poker != null) {
            pokerValue.setText(poker.value);
            switch (poker.type) {
                case GRASS:
                    pokerValue.setTextColor(POKER_BLACK);
                    pokerType.setImageResource(R.drawable.poker_grass);
                    break;
                case SPADES:
                    pokerValue.setTextColor(POKER_BLACK);
                    pokerType.setImageResource(R.drawable.poker_spades);
                    break;
                case HEARTS:
                    pokerValue.setTextColor(POKER_RED);
                    pokerType.setImageResource(R.drawable.poker_hearts);
                    break;
                case BOX:
                    pokerType.setImageResource(R.drawable.poker_box);
                    pokerValue.setTextColor(POKER_RED);
                    break;
            }
        }
    }

    public void show() {
        back.setVisibility(INVISIBLE);
        pokerType.setVisibility(VISIBLE);
        pokerValue.setVisibility(VISIBLE);
    }

    public void showBlack() {
        back.setVisibility(VISIBLE);
    }

    public enum PokerType {
        SPADES(1),//黑桃
        HEARTS(2),//红桃
        GRASS(3),//草花
        BOX(4)   // 方片
        ;

        int value;

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        private PokerType(int type) {
            this.value = type;
        }

        public static PokerType valueOf(int value) {
            for (PokerType pokerType : values()) {
                if (pokerType.getValue() == value) {
                    return pokerType;
                }
            }
            return SPADES;
        }
    }

    public void setPokerBackRes(@DrawableRes int resID){
        pokerBack = resID;
        back.setImageResource(pokerBack);
    }

}
